<?php

class DashboardController extends Controller {

    public $layout = '//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete', // we only allow deletion via POST request
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view'),
                //'users' => array('*'),
                'users' => array('@'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('update'),
                'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete', 'create'),
                //'users' => array('admin'),
                'expression' => '$user->isAdmin()'
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    /**
     * Lists all models.
     */
    public function actionIndex() {

        $balance = $this->getCurrentUserBalance(Yii::app()->user->id);
        $totalearning = $this->getTotalEarnings("C", Yii::app()->user->id);
        $refearning = $this->getTotalReferralEarnings("C", Yii::app()->user->id);
        $genearning = $this->getTotalGenerationEarnings("C", Yii::app()->user->id);
        $totaldeposit = $this->getMemberDeposit(Yii::app()->user->id);
        $totalwithdraw = $this->getMemberTransaction("D", Yii::app()->user->id, NULL);
        $member = Member::model()->findByPk(Yii::app()->user->id);
        $remainingday = $this->getRemainingPackageDay(Yii::app()->user->id);
        
        
        $package = Package::model()->findByPk($member->package_id);
        $this->render('index', array(
            'model' => $member, 
            'package' => $package, 
            'balance' => $balance, 
            'totalearning'=>$totalearning,
            'refearning'=>$refearning,
            'genearning'=>$genearning,
            'totaldeposit'=>$totaldeposit,
            'totalwithdraw'=>$totalwithdraw,
            'remainingday'=>$remainingday,
            ));
    }

    /**
     * Manages all models.
     * 1 active, 0  Blocked , 2 Suspended Members
     */
    
    public function actionAdmin() {
        $start = date("Y-m-d H:i:s");
        $year = date('Y', strtotime($start));
        $week = date('W', strtotime($start));
        $sunday = strtotime($year . 'W' . $week . '0');

        /* Start from sunday */
        $fd = date('Y-m-d');
        $ld = date('Y-m-d');

        /* Start from sunday | this week calculation */
        //$fd = date('Y-m-d', strtotime('+0 Days', $sunday));
        //$ld = date('Y-m-d', strtotime('+6 Days', $sunday));		

        /* this month calculation */
        //$fd = date('Y-01-m');
        //$ld = date('Y-t-m');

        $sql = "select status, count(*) count from member group by status order by status asc";
        $members = Yii::app()->db->createCommand($sql)->queryAll();

        $total_member = 0;
        $active_member = 0;
        $block_member = 0;
        $suspended_member = 0;

        if (!empty($members)) {
            if (isset($members[0]) && !empty($members[0])) {
                $block_member = $members[0]['count'];
            }
            if (isset($members[1]) && !empty($members[1])) {
                 $active_member = $members[1]['count'];
            }
            if (isset($members[2]) && !empty($members[2])) {
                $suspended_member = $members[2]['count'];
            }
        }
        $total_member = $active_member + $block_member + $suspended_member;

        $sql = "SELECT count(distinct(m.username)) deposit FROM member m, member_transaction_history mh
		where m.member_id = mh.member_id and mh.payment_processors_id is not null and (mh.credit <> 0 and mh.credit is not null)";

        $command = Yii::app()->db->createCommand($sql);
        $deposited = $command->queryScalar();
        $not_deposited = $total_member - $deposited;

        $model = new News('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['News']))
            $model->attributes = $_GET['News'];

        $balance = $this->getCurrentUserBalance(Yii::app()->params['defaultUpline']);

        $this->render('admin', array('model' => $model,
            'total_member' => $total_member,
            'active_member' => $active_member,
            'block_member' => $block_member,
            'suspended_member' => $suspended_member,
            'deposited' => $deposited,
            'not_deposited' => $not_deposited,
            'balance' => $balance,));
    }

}
